package com.holly.unit.es.api.enums;

import cn.hutool.core.util.ObjectUtil;

/**
 * 类描述: ElasticSearch的字段类型
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/6 10:26
 */
public enum  EsTypeEnum {

    STRING("string", "字符串类型"),
    TEXT("text", "字符串类型"),
    KEYWORD("keyword", "字符串类型"),
    INTEGER("integer", "整数类型"),
    LONG("long", "整数类型"),
    SHORT("short", "整数类型"),
    BYTE("byte", "整数类型"),
    DOUBLE("double", "浮点类型"),
    FLOAT("float", "浮点类型"),
    HALF_FLOAT("half_float", "浮点类型"),
    SCALED_FLOAT("scaled_float", "浮点类型"),
    BOOLEAN("boolean", "逻辑类型"),
    DATE("date", "日期类型");

    private String type;
    private String name;

    EsTypeEnum(String type, String name) {
        this.type = type;
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public String getName() {
        return name;
    }

    /**
     * 比较两个值是否相等
     */
    public boolean compare(Object value) {
        EsTypeEnum type = EsTypeEnum.getByValue(value);
        return type == this;
    }

    public static EsTypeEnum getByValue(Object value) {
        if (ObjectUtil.isEmpty(value)) {
            return null;
        }
        return getByValue(value.toString());
    }

    public static EsTypeEnum getByValue(String value) {
        if (ObjectUtil.isEmpty(value)) {
            return null;
        }
        for (EsTypeEnum val : values()) {
            if (val.getType().toLowerCase().equals(value.toLowerCase())) {
                return val;
            }
        }
        return null;
    }

}
